package micro.controller.login;

import com.alibaba.fastjson.JSONArray;
import com.google.code.kaptcha.Producer;
import micro.pojo.hr;
import micro.service.login.hrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author Like Old Dream
 * @date 2020/12/22 - 10:41
 */
@Controller
@RequestMapping("/out")
public class logncon {
    private Producer kaptchaProducer = null;
    @Autowired
    private hrService hrserive;
    /*设置验证码*/
    @Autowired
    public void setCaptchaProducer(Producer kaptchaProducer) {
        this.kaptchaProducer = kaptchaProducer;
    }
    /*制作验证码*/
    @RequestMapping("/getVerifyCode")
    public ModelAndView handleRequest(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = kaptchaProducer.createText();
        session.setAttribute("rCode",capText);
        request.getSession().setAttribute("verifyCode", capText);
        BufferedImage bi = kaptchaProducer.createImage(capText);
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @RequestMapping(value = "/jquey",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public Object jquery(hr hr, HttpSession session, HttpServletRequest request, HttpServletResponse response ) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String j_captcha=request.getParameter("j_captcha");
        String checkCode_session =(String) session.getAttribute("rCode");
        HashMap<String,String > resultMap = new HashMap<String,String>();
        System.out.println(username);
        System.out.println(password);
        hr = hrserive.login(username);
        if (hr.getUsername().equals(username)) {
            if (hr.getPassword().equals(password)) {
                System.out.println("996");
                if (hr.getRoleid()!=1) {
                    System.out.println(hr.getRoleid());
                    System.out.println("123111");
                    if (checkCode_session.equals(j_captcha)) {
                        session.removeAttribute(checkCode_session);
                        session.setAttribute("username",hr);
                        System.out.println("成功成任");
                        resultMap.put("username","yes");
                    }else {
                        session.removeAttribute(checkCode_session);
                        System.out.println("错误吗");
                        resultMap.put("username","no");
                    }
                }else if (checkCode_session.equals(j_captcha)) {
                        session.removeAttribute(checkCode_session);
                        session.setAttribute("username",hr);
                        resultMap.put("username","sos");
                    }else {
                        session.removeAttribute(checkCode_session);
                        System.out.println("错误吗");
                        resultMap.put("username","no");
                    }
                }
            }else {
                resultMap.put("username","no");
            }
        return JSONArray.toJSONString(resultMap);
    }
}

